package com.dapeis.web.wx.controller.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ag.utils.FileUtils;
import com.ag.web.controller.BaseController;
import com.dapeis.core.utils.StylistConstants;
/**
 * 菜单设置
 * Title:WxMenuController.java WxMenuController
 * Description:
 * @author yuanyong
 * @date 2017年4月18日 下午1:42:59 
 * Email: hzyuanyong@126.com
 *
 */
@Controller
@RequestMapping("/wx/menu")
public class WxMenuController extends BaseController {

    @Resource
    WxMpService wxMpService;

    @RequestMapping(value = "/delete")
    public @ResponseBody String delete(HttpSession session,
	    HttpServletRequest request, HttpServletResponse response)
	    throws IOException {
	try {
	    wxMpService.menuDelete();
	    return "删除成功.";
	} catch (WxErrorException e) {
	    e.printStackTrace();
	    return "delete失败：" + e.getLocalizedMessage();
	}
    }

    @RequestMapping(value = "/update")
    public @ResponseBody String update(HttpSession session,
	    HttpServletRequest request, HttpServletResponse response)
	    throws IOException {
	WxMenu menu = new WxMenu();
	List<WxMenuButton> buttons = new ArrayList<WxMenu.WxMenuButton>();
	menu.setButtons(buttons);

	WxMenuButton btnNow = new WxMenuButton();
	btnNow.setName("MS搭配师");
	btnNow.setType(WxConsts.BUTTON_VIEW);
	btnNow.setUrl(FileUtils.splicePaths(StylistConstants.getWxDomain(),"/"));
	buttons.add(btnNow);
	/*
	WxMenuButton btnLater = new WxMenuButton();
	btnLater.setName("测试菜单2");
	btnLater.setType(WxConsts.BUTTON_VIEW);
	btnLater.setUrl(FileUtils.splicePaths(StylistConstants.getWxDomain(),"/test2"));
	buttons.add(btnLater);

	WxMenuButton btnMine = new WxMenuButton();
	btnMine.setType(WxConsts.BUTTON_VIEW);
	btnMine.setName("我的");
	buttons.add(btnMine);

	WxMenuButton btnMineOrder = new WxMenuButton();
	btnMineOrder.setName("我的订单");
	btnMineOrder.setType(WxConsts.BUTTON_CLICK);
	btnMineOrder.setKey("my_order");

	WxMenuButton btnMineContact = new WxMenuButton();
	btnMineContact.setType(WxConsts.BUTTON_CLICK);
	// btnMineContact.setType("media_id");
	btnMineContact.setName("联系客服");
	btnMineContact.setKey("customer_service");
	// btnMineContact.setKey("208221769");
	 
	List<WxMenuButton> mineSubButtons = new ArrayList<WxMenu.WxMenuButton>();
	mineSubButtons.add(btnMineOrder);
	// mineSubButtons.add(btnMineScancode);
	mineSubButtons.add(btnMineContact);
	btnMine.setSubButtons(mineSubButtons);
	 */
	// 测试推送菜单
	try {
	    String json = menu.toJson();
	    System.out.println("创建menu：" + json);
	    wxMpService.menuCreate(menu);
	    return json;
	} catch (WxErrorException e) {
	    e.printStackTrace();
	    return "update失败:" + e.getLocalizedMessage();
	}
    }
}